﻿using DevelopTool.Common;
using DevelopTool.Model.TipsModels;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace DevelopTool.Bll.TipsBlls.Jobs
{
    /// <summary>
    /// 事项提醒弹出窗口
    /// </summary>
    public partial class TipsShowForm : UIForm
    {
        private Tips tips;

        private Timer timer;

        public TipsShowForm(Tips tips)
        {
            InitializeComponent();

            this.tips = tips;
        }


        private void TipsShowForm_Load(object sender,EventArgs e)
        {
            if(this.tips.PlayAudio)
                PlayTipsAudio();

            this.lblName.ForeColor = Color.Green;

            this.lblNowTime.Text = DateTime.Now.TimeString();
            this.lblNowTime.Font = new Font("宋体",150);

            this.lblName.Text = this.tips.NodeName;
            this.Text = this.tips.NodeName;

            try
            {
                this.scroText.Text = tips.Text.GZipDecompress();
            }
            catch
            {
                this.scroText.Text = tips.Text;
            }

            //关闭按钮居中设置
            int x = Convert.ToInt32(this.Width / 2);
            int y = Convert.ToInt32(this.Height / 2);
            this.btnClose.Location = new Point(x - 45,y + 400);


            //初始化计时器
            timer = new Timer
            {
                Interval = 1000,
                Enabled = true//启动计时器
            };
            timer.Tick += Timer_Tick;


            UIMenuStyle uIMenuStyle = Setting.Theme.ControlThemColor;
            switch(uIMenuStyle)
            {
                case UIMenuStyle.Black:
                    this.BackColor = ThemHelper.CustomBackColor_Black;
                    this.RectColor = ThemHelper.CustomBackColor_Black;
                    this.scroText.FillColor = ThemHelper.CustomBackColor_Black;
                    break;
                case UIMenuStyle.White:
                    this.BackColor = ThemHelper.CustomBackColor_White;
                    this.RectColor = ThemHelper.CustomBackColor_White;
                    this.scroText.FillColor = ThemHelper.CustomBackColor_White;
                    break;
            }
        }


        private void Timer_Tick(object sender,EventArgs e)
        {
            this.lblNowTime.Text = DateTime.Now.TimeString();

            if(DateTime.Now.Second % 2 == 0)
            {
                var foreColor = ColorHelper.GetRandomColor(false);
                this.lblNowTime.ForeColor = foreColor;
                this.scroText.ForeColor = foreColor;
            }

            if(DateTime.Now.Second % 4 == 0)
            {
                this.lblName.ForeColor = ColorHelper.GetRandomColor(false);
                //绑定动画
                AnimatorHelper.BindShowAnimator(new List<Control>() {
                    { this.btnClose}
                });
            }
        }


        /// <summary>
        /// 播放音频
        /// </summary>
        private void PlayTipsAudio()
        {
            //未指定音频文件时，播放系统默认的通知音频
            if(string.IsNullOrEmpty(this.tips.AudioPath) || !File.Exists(this.tips.AudioPath) || Path.GetExtension(this.tips.AudioPath).ToUpper() != ".WAV")
            {
                SytemSoundHelper.PlaySystemSound("Default");
            }
            else
            {
                WavPlayerHelper.Play(this.tips.AudioPath,WavPlayerHelper.AudioPlayMode.BackgroundLoop);
            }
        }



        /// <summary>
        /// 关闭窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender,EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }


        /// <summary>
        /// 窗体关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TipsShowForm_FormClosing(object sender,FormClosingEventArgs e)
        {
            timer?.Dispose();

            WavPlayerHelper.Stop();
        }

    }
}
